package com.haynes.meetxiaohai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haynes.meetxiaohai.common.R;
import com.haynes.meetxiaohai.entity.Category;
import com.haynes.meetxiaohai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 * @Author Haynes
 * @Date 2023/3/20 10:08
 * @PackageName:com.haynes.reggie.controller
 * @ClassName: CategoryController
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功！");
    }
    @GetMapping("/page")
    public R<Page<Category>> page(int page,int pageSize){
//        创建分页插件对象
        Page<Category> categoryPage =new Page<Category>(page,pageSize);
//        创建条件查询对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getSort);

        categoryService.page(categoryPage,queryWrapper);
        return R.success(categoryPage);
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        categoryService.remove(ids);
        return R.success("分类信息删除成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("分类信息更新成功！");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
//        创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
//        添加条件 当菜品类型不等于null的时候执行，eq：比较条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
//        根据菜品顺序进行升序，根据更新时间进行降序排序
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//        查询数据
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);
    }
}
